<?php
include_once('../../php/postgre.php');
$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];

if ($act == 'simpan_misi'){
	$idvisi = $_POST['id_visi'];
	$idmisi = $_POST['id_misi'];
	$misi = $_POST['misi'];
	
	if(!is_numeric($idmisi)){
		echo "Err: invalid id.";
		exit;
	}
	
	if ($idmisi>0){
		exec_query("update rpjm_misi set id_visi='".$idvisi."' ,misi='".$misi."' where id_misi=".$idmisi);
		echo 'success__';
		
		logs('Mengubah Misi','Misi baru = '.$misi);
	}else{
		$maxid = get_maxid('id_misi', 'rpjm_misi');
		exec_query("insert into rpjm_misi (id_misi,id_visi,misi) values(".$maxid.",".$idvisi.",'".$misi."')");
		$stored = get_data('select id_misi from rpjm_misi where id_misi='.$maxid);
		if($stored['id_misi']==$maxid){
			echo 'success__';
			$idmisi = $maxid;
		}
		
		logs('Menambah Misi','Misi = '.$misi);
	}
	echo $idmisi.'__';
	view_misi();
	
}else if ($act == 'edit_misi'){
	$idmisi=$_POST['idmisi'];
	if(is_numeric($idmisi)){
	$data = get_data ("select m.id_misi, v.id_visi, v.visi, m.misi from rpjm_visi v, rpjm_misi m where m.id_visi=v.id_visi and m.id_misi=".$idmisi);
		
		$ress = implode($data,'__');
		print_r($ress);
		
	}else{
		echo 'error';
	}
}	

function view_misi(){
	$t_misi = get_datas ("select m.id_misi, v.id_visi, v.visi, m.misi from rpjm_visi v, rpjm_misi m where m.id_visi=v.id_visi order by m.id_misi");
	$no=1;
	foreach ($t_misi as $misi){
	?>
                            	<tr>
                                	<td style="text-align:center"><? echo $no;?></td>
                                    <td><? echo $misi ['visi'];?></td>
                                    <td><? echo $misi['misi']?></td>
                                    <td style="text-align:center"><a href="#collapseTwo" data-toggle="collapse" data-parent="#accordion2" class="btn btn-info btn-small bt-edit-misi" name="<?php echo $misi['id_misi']; ?>"><i class="icon-edit icon-white"></i>Ubah</a></td>
                                </tr>
    <?php
		$no++;
	}
}
?>